package com.example.base.view

import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

abstract class BaseActivity <VDB:ViewDataBinding> : AppCompatActivity() {
    protected lateinit var mDataBinding: VDB
    protected var mSaveInstanceState : Bundle? = null
    //参数savedInstanceState：用于获取Activity的事例状态
    //场景：当Activity重新创建（屏幕旋转，配置更新）
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //使用DataBinding设置布局
        mDataBinding = DataBindingUtil.setContentView(this,getLayoutId())
        //初始化
        initView()
    }

    //获取Activity重建后的事例状态参数
    override fun onRestoreInstanceState(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onRestoreInstanceState(savedInstanceState, persistentState)
//        if (savedInstanceState)
        mSaveInstanceState = savedInstanceState!!
        mSaveInstanceState?.run {
            getAcitvityState(this)
        }
    }
    //保存当前Activity的状态（屏幕旋转，配置更新）
    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        saveActivityState(outState)
    }
    protected abstract fun getLayoutId():Int
    protected open fun initView(){

    }
    protected open fun getAcitvityState(state : Bundle){

    }
    protected open fun saveActivityState(outState: Bundle){

    }
}